<?php
use yii\helpers\Url;
?>
<div class="layui-fluid">
    <div class="layui-card">


        <div class="layui-card-body">
            <div style="padding-bottom: 10px;">
                <button class="layui-btn layuiadmin-btn-admin" data-type="batchdel">删除</button>
                <button class="layui-btn layuiadmin-btn-admin" data-type="add">添加</button>

            </div>

            <table id="LAY-user-back-manage" lay-filter="LAY-user-back-manage"></table>

            <script type="text/html" id="create_time">
                {{layui.util.toDateString(d.create_time*1000)}}
            </script>
            <script type="text/html" id="buttonTpl">
                <input type="checkbox" {{ d.is_show == 1 ? 'checked':'' }} name="status" lay-skin="switch" value="{{d.is_show}}" lay-filter="isShowModel" switchId={{d.id}} lay-text="显示|隐藏">

            </script>
            <script type="text/html" id="table-useradmin-admin">
                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
            </script>
        </div>
    </div>
</div>

<?php $this->beginBlock('footer')?>
<script>
    function edit(){
        alert(2)
    }
    layui.config({
        base: '<?=Url::base()?>/resources/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口菜单
    }).use(['index', 'useradmin', 'table'], function(){
        var $ = layui.$
            ,form = layui.form
            ,table = layui.table;
//管理员管理
        table.render({
            elem: '#LAY-user-back-manage'
            ,url: '<?=Url::toRoute([$this->context->id.'/list','menuId'=>$menuId])?>' //模拟接口
            ,limit: '<?=$pageSize?>' //每页默认显示的数量
            ,page: true
            ,cols: [[
                {type: 'checkbox', fixed: 'left'}
                ,{field: 'id', width: 80, title: 'ID', sort: true}
                ,{field: 'route_name', title: '子路由'}
                ,{field: 'route', title: '路由'}
                ,{field: 'sort', title: '排序'}
                ,{field: 'is_show', title:'状态', templet: '#buttonTpl', minWidth: 80, align: 'center'}
                ,{field: 'create_time', title: '加入时间',templet: '#create_time', sort: true}
                ,{title: '操作', width: 230, align: 'center', fixed: 'right', toolbar: '#table-useradmin-admin'}
            ]]
            ,text:{success:'对不起，加载出现异常！'}
        });
        //监听搜索
        form.on('submit(LAY-user-back-search)', function(data){
            var field = data.field;
            //执行重载
            table.reload('LAY-user-back-manage', {
                where: field
            });
        });

        //监听开关事件
        form.on('switch(isShowModel)',function (obj) {
            console.log(obj);
            let params = {};
            params['status'] = obj.value;
            params['id'] = obj.elem.getAttribute('switchId');
            params['_csrf-backend'] = '<?=Yii::$app->request->csrfToken?>';
            $.post('<?=Url::toRoute([$this->context->id.'/status'])?>',params,function (res) {
                layer.msg(res.msg, {icon:1,time: 1500})
            },'json')
        });

        //事件
        var active = {
            batchdel: function(){
                var checkStatus = table.checkStatus('LAY-user-back-manage')
                    ,checkData = checkStatus.data; //得到选中的数据

                if(checkData.length === 0){
                    return layer.msg('请选择数据');
                }

                layer.prompt({
                    formType: 1
                    ,title: '敏感操作，请验证口令'
                }, function(value, index){
                    layer.close(index);

                    layer.confirm('确定删除吗？', function(index) {

                        //执行 Ajax 后重载
                        /*
                        admin.req({
                          url: 'xxx'
                          //,……
                        });
                        */
                        table.reload('LAY-user-back-manage');
                        layer.msg('已删除');
                    });
                });
            }
            ,add: function(){
                layer.open({
                    type: 2
                    ,title: '添加子路由'
                    ,content: '<?=Url::toRoute([$this->context->id.'/create','menuId'=>$menuId])?>'
                    ,area: ['420px', '420px']
                    ,btn: ['确定', '取消']
                    ,yes: function(index, layero){
                        var iframeWindow = window['layui-layer-iframe'+ index]
                            ,submitID = 'module-submit'
                            ,submit = layero.find('iframe').contents().find('#'+ submitID);
                        //监听提交
                        iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                            var field = data.field; //获取提交的字段
                            $.post('<?=Url::toRoute([$this->context->id.'/create'])?>',field,function (res) {
                                if(res.status==200){
                                    table.reload('LAY-user-back-manage'); //数据刷新
                                    layer.close(index); //关闭弹层
                                    layer.msg(res.msg,{icon:1,time:1500},function () {

                                    })
                                }else {
                                    layer.msg(res.msg,{icon:2,time:1500});
                                }
                            },'json')
                        });

                        submit.trigger('click');
                    }
                });
            }
        }
        $('.layui-btn.layuiadmin-btn-admin').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        table.on('tool(LAY-user-back-manage)', function(obj){
            var data = obj.data;
            var id = data.id;
            if(obj.event === 'del'){
                layer.confirm('确定删除该条数据？', function(index){
                    $.get('<?=Url::toRoute([$this->context->id.'/del'])?>',{id:id},function (res) {
                        if(res.status==200){
                            layer.msg(res.msg,{icon:1,time:1000},function () {
                                console.log(obj);
                                obj.del();
                                layer.close(index);
                            })
                        }else {
                            layer.msg(res.msg,{icon:2,time:1000})
                        }
                    },'json')

                });
            }else if(obj.event === 'edit'){
                var tr = $(obj.tr);
                layer.open({
                    type: 2
                    ,title: '编辑菜单'
                    ,content: '<?=Url::toRoute([$this->context->id.'/edit'])?>?id='+id
                    ,area: ['420px', '420px']
                    ,btn: ['确定', '取消']
                    ,yes: function(index, layero){
                        var iframeWindow = window['layui-layer-iframe'+ index]
                            ,submitID = 'module-edit-submit'
                            ,submit = layero.find('iframe').contents().find('#'+ submitID);

                        //监听提交
                        iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                            var field = data.field; //获取提交的字段
                            $.post('<?=Url::toRoute([$this->context->id.'/edit'])?>',field,function (res) {
                                if(res.status==200){
                                    table.reload('LAY-user-back-manage'); //数据刷新
                                    layer.close(index); //关闭弹层
                                    layer.msg(res.msg,{icon:1,time:1500},function () {

                                    })
                                }else {
                                    layer.msg(res.msg,{icon:2,time:1500});
                                }
                            },'json')
                        });
                        submit.trigger('click');
                    }
                    ,success: function(layero, index){

                    }
                })
            }
        });
    });
</script>

<?php $this->endBlock()?>


